<?php
namespace wof;

use wof\Cat;
use wof\Quiz;
use wof\Letter;

class Game {
    protected $msg;
    public $a_PlayerData = array();
    public $i_ConnId;
    
    public function __construct($msg, $a_PlayerData, $i_ConnId) {
        $this->msg = $msg;
        $this->a_PlayerData = $a_PlayerData;
        $this->i_ConnId = $i_ConnId;
    }
    
    /* Start check the command */
    public function start () {
        $a_msg_elements = explode("%%C%%",$this->msg);
        return $this->control($a_msg_elements[0], $a_msg_elements[1]);
    }
    
    /* controller */
    public function control ($command, $value) {
        // Check the legal command based on player turn and command store in data
        // echo "\nplay_turn->" . $this->a_PlayerData['play_turn'] . "\n";
        // echo "\ni_ConnId->" . $this->i_ConnId  . "\n";
        // echo "\nnext_action->" . $this->a_PlayerData['next_action'] . "\n";
        // echo "\ncommand->" . $command  . "\n";
        if ( $command != "user" && $this->a_PlayerData['status'] == "play" && 
            (( $this->a_PlayerData['play_turn'] && $this->a_PlayerData['play_turn'] != $this->i_ConnId )
            || ( $this->a_PlayerData['next_action'] && $this->a_PlayerData['next_action'] != $command )))
        {
            // Return the error
            $a_Ret = array();
            $a_Ret['control'] = 'error';
            $a_Ret['value'] = "Illegal command!";
            return $a_Ret;
        }
        else {
            switch ($command) {
                case 'ready':
                    return $this->ready($value);
                    break;
                case 'spin':
                    return $this->spin($value);
                    break;
                case 'score':
                    return $this->score($value);
                    break;
                case 'letter':
                    return $this->letter($value);
                    break;
                case 'user':
                    return $this->user($value);
                    break;
                case 'vowel':
                    return $this->getVowel($value);
                    break;
                case 'turn':
                    return $this->loseturn();
                    break;
                case 'endround':
                    return $this->endround();
                    break;
            }
        }
    }
    
    /* Prepare game */
    public function ready () {
        if ($this->a_PlayerData['status'] != "play") {
            $a_Ret = array();
            
            /* Init data */
            $this->a_PlayerData['status'] = "play";
            $this->a_PlayerData['round'] = 1;
            $this->a_PlayerData['play_turn'] = $this->a_PlayerData['a_Player'][0]['connId'];
            $this->a_PlayerData['play_turn_name'] = $this->a_PlayerData['a_Player'][0]['username'];
            $this->a_PlayerData['next_action'] = "spin";
            $this->a_PlayerData["cat-id"] = "";
            $this->a_PlayerData["select-quiz-id"] = array();
            $this->a_PlayerData["quiz-answer"] = "";
            $this->a_PlayerData["a_LetterMatrix"] = array();
            
            // Return the waitting time
            $a_Ret['control'] = 'ready';
            $a_Ret['value'] = 15;
        }
        else {
            // Return the error
            $a_Ret['control'] = 'error';
            $a_Ret['value'] = "Illegal command!";
        }
        return $a_Ret;
    }
    
    /* Spin wheel */
    public function spin ($value) {
        $a_Ret = array();
        $a_Ret['control'] = 'spin';
        $a_Ret['value'] = $value;
        $this->a_PlayerData['next_action'] = "score";
        return $a_Ret;
    }
    
    /* Process score */
    public function score ($value) {
        if ($this->a_PlayerData['play_turn'] == $this->i_ConnId) {
            $a_Ret = array();
            $this->a_PlayerData['temp_score'] = 0;
            switch ($value) {
                case "Bankrup" :
                    $a_Ret = $this->bankrup();
                    return $a_Ret;
                    break;
                case "Buy a vowel" :
                    $this->buyvowel();
                    break;
                case "Lose a turn" :
                    $a_Ret = $this->loseturn();
                    return $a_Ret;
                    break;
                case "Free Spin":
                    $this->freespin();
                    break;
                default:
                    $this->a_PlayerData['temp_score'] = $value;
                    $this->a_PlayerData['next_action'] = "letter";
            }
        }
    }

    /* Get Letter Position */
    public function letter ($value) {
        $a_Ret = array();
        $o_Letter = new Letter($this->a_PlayerData, $this->i_ConnId);
        $a_Ret = $o_Letter->i_fPickup($value);
        $this->a_PlayerData = $o_Letter->a_PlayerData;
        return $a_Ret;
    }

    /* Get Question Category */
    public function cat () {
        $a_Ret = array();
        $o_Cat = new Cat;
        $a_Ret = $o_Cat->generate_cat();
        return $a_Ret;
    }

    /* Get Question */
    public function quiz ($i_CatId, $a_SelectedQuiz = array()) {
        $a_Ret = array();
        $o_Quiz = new Quiz($i_CatId, $this->a_PlayerData);
        $a_Ret['control'] = 'quiz';
        $a_Ret['value'] = $o_Quiz->generate_quiz($a_SelectedQuiz);
        $this->a_PlayerData = $o_Quiz->a_PlayerData;
        return $a_Ret;
    }
    
    /* Validate user log-on and check limitation of player then send to clients */
    public function user ($value) {
        $a_Ret = array();
        $o_User = new User($this->a_PlayerData);
        $a_Ret = $o_User->validateUser($this->i_ConnId, $value);
        $this->a_PlayerData = $o_User->a_PlayerData;
        return $a_Ret;
    }
    
    /* Get Vowel */
    public function getVowel ($value) {
        $a_Ret = array();
        $o_Letter = new Letter($this->a_PlayerData, $this->i_ConnId);
        $a_Ret['control'] = 'vowel';
        $a_Ret['value'] = $o_Letter->i_fSelect($value);
        $this->a_PlayerData = $o_Letter->a_PlayerData;
        return $a_Ret;
    }
    
    /* Clear player score and give the turn to next user */
    public function bankrup () {
        $a_Ret = array();
        for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
            if ((int)$this->a_PlayerData['a_Player'][$i]['connId'] == (int)$this->a_PlayerData['play_turn']) {
                $this->a_PlayerData['a_Player'][$i]['score'] = 0;
                /* This user is last player in player list*/
                if ($i == (count($this->a_PlayerData['a_Player']) -1)) {
                    $this->a_PlayerData['play_turn'] = $this->a_PlayerData['a_Player'][0]['connId'];
                    $this->a_PlayerData['play_turn_name'] = $this->a_PlayerData['a_Player'][0]['username'];
                }
                else {
                    $this->a_PlayerData['play_turn'] = $this->a_PlayerData['a_Player'][$i+1]['connId'];
                    $this->a_PlayerData['play_turn_name'] = $this->a_PlayerData['a_Player'][$i+1]['username'];
                }
                break;
            }
        }
        $this->a_PlayerData['next_action'] = "spin";
        // Activate turn
        $a_Ret['control'] = 'turn';
        $a_Ret['value'] = $this->a_PlayerData['play_turn_name'];
        return $a_Ret;
    }
    
    /* Set next action for buy a vowel */
    public function buyvowel () {
        $this->a_PlayerData['next_action'] = "vowel";
    }
    
    /* Lose a turn */
    public function loseturn () {
        $o_Letter = new Letter($this->a_PlayerData, $this->i_ConnId);
        $a_Ret = array();
        $a_Ret = $o_Letter->loseturn();
        $this->a_PlayerData = $o_Letter->a_PlayerData;
        return $a_Ret;
    }
    
    /* Set next action for buy a vowel */
    public function freespin () {
        $this->a_PlayerData['next_action'] = "letter";
    }
    
    /* End Round */
    public function endround () {
        $a_Ret = array();
        $a_Ret['control'] = 'endround';
        $a_Ret['value'] = '';
        return $a_Ret;
    }
}

?>